<?php
namespace app\admin\controller;
use think\Controller;

class Base extends Controller
{
    public function initialize(){
        $this->_initialize();
    }

    public function _initialize(){
        $this->readConfig();

        $key = Config('USER_AUTH_KEY');
        $authPath = strtolower(Request()->module().'/'.Request()->controller().'/'.Request()->action());
        //exit($authPath);
        if($authPath=='admin/member/login'){

        }else{
            if(Session('?'.$key)!=null) {
                $uid = Session($key);
                $this->assign('userinfo',model('User')->getView(['id'=>$uid]));
            }else{
                $this->redirect(__APP__.'/Member/login.html');
            }
        }
    }
    protected function readConfig($refrash=false){
        $list=Cache('Config');
        if (!$list || $refrash) {
            $config = model("Config");
            $groups = $config->distinct(true)->column('category');
            $list = array();
            foreach ($groups as $value){
                $_tmp = $config->where('category',$value)->column('name,value');
                $list[$value] = array_change_key_case($_tmp,CASE_LOWER);
            }
            $list=array_change_key_case($list,CASE_LOWER);
            // 所有配置参数统一为大写
            Cache('Config',$list);
        }
        foreach ($list as $key=>$value){
            Config($value,$key);
        }
    }
    public function index()
    {
        return view('index');
    }
    public function edit()
    {
        return view('edit');
    }
	public function del(){
        $model = model(request()->controller());
        $ids = input('ids');
        if(is_string($ids)){
            $ids=[$ids];
        }
        $model->whereIn('id',$ids)->delete();

        $this->success('操作成功！'.$model->getlastsql());
	}
    public function add()
    {
        return view('edit');
    }
    public function save()
    {
        $this->update();
    }
    public function update(){
        $model = model(request()->controller());
        $pk = $model->getPk();
        $id = input('post.'.$pk);
        $data = input('post.');
        if(method_exists($this, '_filterData')){
            $data =  $this->_filterData($data);
        }
        $result = $this->validate($data,'app\\common\\validate\\'.request()->controller());
        if (true !== $result) {
            $this->error($result);
        }
        if($id){
            $result = $model->allowField(true)->save($data,[$pk=>$id]);
        }else{
            $result = $model->allowField(true)->save($data);
        }
        if($result){
            $this->success('保存成功！');
        }else{
            $msg = $model->getError();
            if($msg){
                $this->error($msg);            
            }else{
                $this->error('保存失败，请稍后重试！'.$model->getlastsql());
            }
        }
    }
    public function attr(){
        $field = input('field');
        $id = input('ids');
        $val = input('val');
        $model = model(request()->controller());
        $model->allowField([$field])->save([$field=>$val],['id'=>$id]);
        //exit($model->getlastsql());
        $this->success('操作成功！');
        //P($val);
        //exit;
    }
}
